<template>
    <el-card>
         <el-form :model="queryParams" ref="queryParams" :inline="true" label-width="120px">
            <el-form-item label="服务名：" prop="service">
                <el-input v-model="queryParams.service" placeholder="服务名" size="small"></el-input>
            </el-form-item>
             <el-form-item label="描述：" prop="description">
                <el-input v-model="queryParams.description" placeholder="描述" size="small"></el-input>
            </el-form-item>
            <el-form-item label="服务类型：" prop="type">
                <el-select v-model="queryParams.type" placeholder="服务类型" size="small">
                    <el-option v-for="item in typeArr" :label="item.label" :key="item.value" :value="item.value"></el-option>
                  </el-select>
            </el-form-item>
             <el-form-item label="版本：" prop="version">
                <el-input v-model="queryParams.version" placeholder="版本" size="small"></el-input>
            </el-form-item>
             <el-form-item label="分组：" prop="group">
                <el-input v-model="queryParams.group" placeholder="分组" size="small"></el-input>
            </el-form-item>
             <el-form-item >
                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-delete" size="small" @click="resetQuery('queryParams')">清空</el-button>
            </el-form-item>
         </el-form>
         <el-row :gutter="10">
                <el-col :span="24">
                    <div style="margin-bottom: 10px;padding: 0 10px">
                        <el-button type="primary" icon="el-icon-plus" size="small" @click="handleAdd">新增</el-button>
                    </div>
                </el-col>
        </el-row>
        <el-table :data="tableData" size="mini" border style="width: 100%">
            <el-table-column type="index" label="#" width="55"></el-table-column>
            <el-table-column prop="service" label="服务名"></el-table-column>
            <el-table-column prop="description" label="描述"></el-table-column>
            <el-table-column prop="type" label="服务类型">
                <template slot-scope="{row}">
                    {{row.type|typeFilter}}
                </template>
            </el-table-column>
            <el-table-column prop="version" label="版本"></el-table-column>
            <el-table-column prop="group" label="分组"></el-table-column>
            <el-table-column prop="instance" label="实例列表"></el-table-column>
            <el-table-column prop="loadBalanceType" label="负载均衡">
                <template slot-scope="{row}">
                    <span v-if="row.loadBalanceType===1">轮询</span>
                    <span v-if="row.loadBalanceType===2">随机</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="200" align="center">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                v-show="total>0"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                background
                :current-page="1"
                :page-sizes="[10, 20, 30, 40,50,100]"
                :page-size="10"
                layout="total, sizes, prev, pager, next, jumper"
                class="pagination-container"
                :total="total">
        </el-pagination>
        <el-dialog :title="formTitle" :visible.sync="addFormVisible">
            <el-form :model="form" :rules="rules" ref="form">
                <el-form-item  prop="type" label="服务类型：" required :label-width="formLabelWidth">
                    <el-radio-group v-model="form.type">
                        <el-radio v-for="item in typeArr" :label="item.value" :key="item.value">{{item.label}}</el-radio>
                    </el-radio-group>
                    <div class="tips" v-if="form.type===3">只需填写不支持服务发现的HTTP服务</div>
                </el-form-item>
                <el-form-item  prop="service" label="服务名：" required :label-width="formLabelWidth">
                    <el-input v-model="form.service" autocomplete="off" size="mini" placeholder="请输入服务名"></el-input>
                    <div class="tips" v-if="form.type===1">服务名须包含包路径，如：com.abc.UserService; 保存后服务名不能修改</div>
                    <div class="tips" v-if="form.type===2">服务名须包含包路径，如：com.abc.UserService，gRPC服务需启用反射功能(样例) ; 保存后服务名不能修改</div>
                    <div class="tips" v-if="form.type===3">服务名可自定义, 网关会根据该名称来获取实例的IP和端口，如：OrderService; 保存后服务名不能修改</div>
                </el-form-item>
                 <el-form-item  prop="description" label="描述：" :label-width="formLabelWidth">
                    <el-input type="textarea" rows="3" v-model="form.description" autocomplete="off" size="mini" placeholder="请输入描述"></el-input>
                 </el-form-item>
                <el-form-item  prop="version" label="版本：" :label-width="formLabelWidth" v-if="form.type===1">
                    <el-input v-model="form.version" autocomplete="off" size="mini" placeholder="请输入版本"></el-input>
                    <div class="tips">Dubbo接口的版本号</div>
                </el-form-item>
                <el-form-item  prop="group" label="分组：" :label-width="formLabelWidth" v-if="form.type===1">
                    <el-input v-model="form.group" autocomplete="off" size="mini" placeholder="请输入分组"></el-input>
                    <div class="tips">Dubbo接口的分组</div>
                </el-form-item>
                <el-form-item  prop="instance" label="实例列表：" :label-width="formLabelWidth" required v-if="form.type!==1">
                    <el-input type="textarea" rows="3" v-model="form.instance" autocomplete="off" size="mini" placeholder="如：10.2.3.114:8080或user.fizzgate.com:9090"></el-input>
                 </el-form-item>
                <el-form-item  prop="loadBalanceType" label="负载均衡：" :label-width="formLabelWidth" required v-if="form.type!==1">
                    <el-radio-group v-model="form.loadBalanceType">
                        <el-radio v-for="item in loadBalanceArr" :label="item.value" :key="item.value">{{item.label}}</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm('form')" size="small">保存</el-button>
                <el-button @click="addFormVisible = false" size="small">取消</el-button>
            </div>
        </el-dialog>
    </el-card>
</template>

<script>
import { getRpcServiceList,save,remove } from '@/api/rpc/service.api.js'
    export default {
        data() {
            return {
                queryParams: {
                    current: 1,
                    size: 10,
                    service: undefined,
                    description: undefined,
                    type: undefined,
                    version: undefined,
                    group: undefined

                },
                typeArr: [
                    {label: 'Dubbo',value: 1},
                    {label: 'gRPC',value: 2},
                    {label: 'HTTP',value: 3},
                ],
                loadBalanceArr: [
                    {label: '轮循',value: 1},
                    {label: '随机',value: 2},
                ],
                tableData: [],
                total: 0,
                form: {
                    type: 1,
                    loadBalanceType: 1
                },
                rules: {
                    type: [{ required: true, message: '请选择服务类型', trigger: 'blur' }],
                    service: [{ required: true, message: '请输入服务名', trigger: 'blur' }],
                    instance: [{ required: true, message: '请输入实例列表', trigger: 'blur' }],
                },
                addFormVisible: false,
                formLabelWidth: '120px',
                formTitle: '新增'
            }
        },
        filters: {
            typeFilter(value) {
                if(value===1) {
                    return 'Dubbo';
                }else if(value===2) {
                    return 'gRPC';
                }else if(value===3) {
                    return 'HTTP';
                }
            }
        },
        mounted() {
            this.getList();
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    const result = await save(this.form);
                    if(result.code === 200) {
                        this.$message.success('保存成功!');
                        this.addFormVisible=false;
                        this.getList();
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
                });
            },
            handleEdit(row) {
                this.form = JSON.parse(JSON.stringify(row));
                this.formTitle = '编辑';
                this.addFormVisible = true;
            },
            handleDelete(id) {
                this.$confirm('确定将选择的数据删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                    }).then(async () => {
                        const result = await remove(id);
                        if(result.code === 200) {
                             this.$message({
                                type: 'success',
                                message: '删除成功!'
                           });
                           this.getList();
                        }
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });          
                });
            },
            getList() {
                getRpcServiceList(this.queryParams).then(res=>{
                    if(res.code === 200) {
                        this.tableData = res.data.records;
                        this.total = res.data.total;
                    }
                })
            },
            // 搜索
            handleQuery() {
                this.queryParams.current = 1;
                this.getList();
            },
            // 清空
            resetQuery(formName) {
                this.$refs[formName].resetFields();
                this.getList();
            },
            handleSizeChange(val) {
                this.queryParams.size = val;
                this.getList();
           },
            handleCurrentChange(val) {
                this.queryParams.current = val;
                this.getList();
            },
            handleAdd() {
                this.addFormVisible = true;
            }
        }
    }
</script>

<style lang="scss" scoped>
.pagination-container {
    padding: 10px;
    text-align: right;
}
.tips {
    color: #999;
    font-size: 12px;
    line-height: normal;
    margin-top: 8px;
    display: block; 
}
</style>